package com.example.demo1.controller.rest;

import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.entity.BaRole;
import com.example.demo1.service.impl.service.RoleService;
import com.example.demo1.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/role")
public class RoleRestController {

    private static Logger logger = LoggerFactory.getLogger(RoleRestController.class);

    @Autowired
    private RoleService roleService;

    @GetMapping("/queryRoles")
    public ServerResponse queryRoles(String roleName,
                                     HttpServletRequest request,
                                     @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){

        logger.info("时间：" + new Date() + "===============调用RoleRestController.queryRoles方法");
        return roleService.queryRoles(roleName, pageNum, pageSize);
    }

    @PostMapping("/addRoleInfo")
    public ServerResponse addRoleInfo(BaRole role, String functionIds, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用RoleRestController.addRoleInfo方法");

        Integer companyId, userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("companyId")) && !StringUtils.isEmpty(decodeResult.get("userId"))){
                companyId = Integer.valueOf(decodeResult.get("companyId").toString());
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用RoleRestController.addRoleInfo方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return roleService.addRoleInfo(role, userId, functionIds);
    }

    @PostMapping("/editRoleInfo")
    public ServerResponse editRoleInfo(BaRole role, String functionIds, HttpServletRequest request){

        logger.info("时间：" + new Date() + "===============调用RoleRestController.editRoleInfo方法");

        Integer companyId, userId;

        try {
            Map<String, Object> decodeResult = JwtUtil.dececodeToken(request.getHeader("Authorization"));
            if(!StringUtils.isEmpty(decodeResult.get("companyId")) && !StringUtils.isEmpty(decodeResult.get("userId"))){
                companyId = Integer.valueOf(decodeResult.get("companyId").toString());
                userId = Integer.valueOf(decodeResult.get("userId").toString());
            }else{
                logger.error("时间：" + new Date() + "==================调用RoleRestController.editRoleInfo方法token解析出错");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
            }
        } catch (Exception e) {
            logger.error(e.toString());
            return ServerResponse.createByErrorCodeMessage(ResponseCode.TOKEN_DATED.getCode(), ResponseCode.TOKEN_DATED.getDesc());
        }

        return roleService.editRoleInfo(role, userId, functionIds);
    }

    @PostMapping("/deleteRoleInfo")
    public ServerResponse deleteRoleInfo(String roleIds){

        logger.info("时间：" + new Date() + "===============调用RoleRestController.deleteRoleInfo方法");

        return roleService.deleteRoleInfo(roleIds);
    }

}
